package com.tmc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.constant.FareConstant.PolicyStatus;
import com.tmc.entity.intair.IntairChaseCabin;
import com.tmc.service.intair.IntairChaseCabinProcessServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/chasecabin")
public class ChaseCabinController extends BaseController{

	@Autowired
	private IntairChaseCabinProcessServiceImpl chaseCabinService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findList(IntairChaseCabin cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<IntairChaseCabin> page = chaseCabinService.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/tool/chasecabin-list", map);
	}
	
	@RequestMapping(value = "/current", method = RequestMethod.GET)
	public ModelAndView findCurrent(IntairChaseCabin cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<IntairChaseCabin> page = chaseCabinService.findCurrentByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/tool/chasecabin-current", map);
	}
	
	@RequestMapping(value = "/history", method = RequestMethod.GET)
	public ModelAndView findHistory(IntairChaseCabin cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<IntairChaseCabin> page = chaseCabinService.findHistoryByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/tool/chasecabin-history", map);
	}
	
	@RequestMapping(value = "/savePage")
	public ModelAndView editPage(Long chaseCabinId,HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		IntairChaseCabin chaseCabin = null;
		if (chaseCabinId != null)
			chaseCabin = chaseCabinService.getById(new ContextInfo(), chaseCabinId);
		else
			chaseCabin = new IntairChaseCabin();
		map.put("chaseCabin", chaseCabin);
		map.put("context", context);
		return new ModelAndView("views/tool/chasecabin-edit", map);
	}
	
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(IntairChaseCabin chaseCabin) {
		CommonResult cr = chaseCabinService.save(new ContextInfo(), chaseCabin);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete{chaseCabinId}")
	@ResponseBody
	public String policyDelete(Long chaseCabinId) {
		CommonResult cr = chaseCabinService.delete(new ContextInfo(), chaseCabinId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/deleteAll{chaseCabinIds}")
	@ResponseBody
	public String policysDelete(String chaseCabinIds) {
		CommonResult cr = chaseCabinService.deletes(new ContextInfo(), chaseCabinIds);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/puton{chaseCabinId}")
	@ResponseBody
	public String policyPutOn(Long chaseCabinId) {
		CommonResult cr = chaseCabinService.changeStatus(new ContextInfo(), chaseCabinId, PolicyStatus.UP.getStatus());
		if (cr.isSuccess()){
			cr.setMessage("已发布！");
		}
		return cr.toJson();
	}
	
	@RequestMapping(value = "/putons{chaseCabinIds}")
	@ResponseBody
	public String policysPutOn(String chaseCabinIds) {
		CommonResult cr = chaseCabinService.changeStatus(new ContextInfo(), chaseCabinIds, PolicyStatus.UP.getStatus());
		if (cr.isSuccess()){
			cr.setMessage("已发布！");
		}
		return cr.toJson();
	}
	
	@RequestMapping(value = "/pulloff{chaseCabinId}")
	@ResponseBody
	public String policyPullOff(Long chaseCabinId) {
		CommonResult cr = chaseCabinService.changeStatus(new ContextInfo(), chaseCabinId, PolicyStatus.DOWN.getStatus());
		if (cr.isSuccess()){
			cr.setMessage("已下架！");
		}
		return cr.toJson();
	}
	
	@RequestMapping(value = "/pulloffs{chaseCabinIds}")
	@ResponseBody
	public String policysPullOff(String chaseCabinIds) {
		CommonResult cr = chaseCabinService.changeStatus(new ContextInfo(), chaseCabinIds, PolicyStatus.DOWN.getStatus());
		if (cr.isSuccess()){
			cr.setMessage("已下架！");
		}
		return cr.toJson();
	}
}
